﻿using System.Collections.Generic;
using System.Text;
using ComHlp.ContentClasses;
using ComHlp.ContentClasses.VirtualControls.MachineUsage;

namespace ComHlp.Classes.Parser.Supplementary
{
    internal class MachineUsageParser
    {
        internal int ParseMachineUsage(StringBuilder sbRecvStr, MapGraphics mapGraphics, int elementLength)
        {
            /* MeldTyp = KV1 (KapazitätsVerteilung1)
                LfdNr		9(01)	fix „1“ 
                Y-Max		9(02) bezogen auf diese Auswahl (not used)
                Y-Lfd		9(02) lfd-Seite oben/unten (not used)
                X-Max		9(02) bezogen auf diese Auswahl (not used)
                X-Lfd		9(02) lfd-Seite links/rechts (not used)
                Y-AbsMax	9(02) absolutes vorgesehenes Maximum
                X-AbsMax	9(02) absolutes vorgesehenes Maximum
                I-Y-ANZ		9(02) Anzahl Zeilen (Werk, etc.)
                I-X-ANZ		9(02) Anzahl Blöcke pro Zeile
                I-Y-ANZ * I-X-ANZ mal:
                Block-Lfd	9(02)
                Ebene		9(01) 1 bis 4
                Proz		9(03) Prozent Belegung
                Farbe		X(01) space=grau (undefiniert); R=red; G=green; Y=yellow
                AnzMasch	9(03) Anzahl Maschinen
                AnzH		9(03) Anzahl Stunden
                I-X-ANZ-mal-ende
                DatumsLeiste	I-X-ANZ mal X(05)
                Text pro Werk	I-Y-ANZ mal (20 = Bez; 1= +/-; 3 Reserve)
                LfdSel		9(03) der per Mouse selektierte Block
            */
            // ReSharper disable once UseObjectOrCollectionInitializer
            ItxMachineUsageHead machineUsageHead = new ItxMachineUsageHead();
            machineUsageHead.Number = ParseHelper.String2Int(sbRecvStr.ToString(0, 1));
            machineUsageHead.MaxY = ParseHelper.String2Int(sbRecvStr.ToString(1, 2));
            machineUsageHead.CurrentY = ParseHelper.String2Int(sbRecvStr.ToString(3, 2));
            machineUsageHead.MaxX = ParseHelper.String2Int(sbRecvStr.ToString(5, 2));
            machineUsageHead.CurrentX = ParseHelper.String2Int(sbRecvStr.ToString(7, 2));
            machineUsageHead.AbsoluteMaxY = ParseHelper.String2Int(sbRecvStr.ToString(9, 2));
            machineUsageHead.AbsoluteMaxX = ParseHelper.String2Int(sbRecvStr.ToString(11, 2));
            machineUsageHead.NumberOfRows = ParseHelper.String2Int(sbRecvStr.ToString(13, 2));
            machineUsageHead.NumberOfColumns = ParseHelper.String2Int(sbRecvStr.ToString(15, 2));
            sbRecvStr.Remove(0, 17);
            elementLength -= 17;

            /* parse usage foreach machine
             * 038R260536
             * 039R260536
             * 017R251704
             * 031R260912
             */
            var usages = new List<ItxSingleMachineUsage>(machineUsageHead.NumberOfRows * machineUsageHead.NumberOfColumns);
            for (int i = 0; i < machineUsageHead.NumberOfRows * machineUsageHead.NumberOfColumns; i++)
            {
                ItxSingleMachineUsage singleMachine = new ItxSingleMachineUsage();
                singleMachine.Usage = ParseHelper.String2Int(sbRecvStr.ToString(0, 3));
                switch (sbRecvStr[3])
                {
                    case ' ':
                        singleMachine.Color = EItxMachineColor.GreyUndefined;
                        break;
                    case 'R':
                        singleMachine.Color = EItxMachineColor.Red;
                        break;
                    case 'G':
                        singleMachine.Color = EItxMachineColor.Green;
                        break;
                    case 'Y':
                        singleMachine.Color = EItxMachineColor.Yellow;
                        break;
                }
                singleMachine.MachineCount = ParseHelper.String2Int(sbRecvStr.ToString(4, 3));
                singleMachine.HoursCount = ParseHelper.String2Int(sbRecvStr.ToString(7, 3));
                usages.Add(singleMachine);

                sbRecvStr.Remove(0, 10);
                elementLength -= 10;
            }

            /* no parse the received days
             * 24.0525.0526.0527.0528.05
             */
            machineUsageHead.DaysForDisplay = new List<string>(machineUsageHead.NumberOfColumns);
            for (int i = 0; i < machineUsageHead.NumberOfColumns; i++)
            {
                string date = sbRecvStr.ToString(0, 5);
                machineUsageHead.DaysForDisplay.Add(date);

                sbRecvStr.Remove(0, 5);
                elementLength -= 5;
            }

            /* now parse the machine names - and parse all other data together (usage above)
             * Text pro Werk	I-Y-ANZ mal (20 = Bez; 1= +/-; 3 Reserve)
             * WWWWWWWWW1          1+   000
             */
            int parsedUsageOffset = 0;
            for (int i = 0; i < machineUsageHead.NumberOfRows; i++)
            {
                ItxMachineUsageDisplay machineUsageDisplay = new ItxMachineUsageDisplay();

                // get the name
                string name;
                if (ComStatics.UseUtf8)
                    name = ParseHelper.ExtractUtf8(20, sbRecvStr, true);
                else
                {
                    name = sbRecvStr.ToString(0, 20);
                    sbRecvStr.Remove(0, 20);
                }
                machineUsageDisplay.Name = name.TrimEnd();
                elementLength -= name.Length;

                // rest of data
                machineUsageDisplay.Id = ParseHelper.String2Int(sbRecvStr.ToString(0, 1));

                // kw 06.06.16
                //machineUsageDisplay.HasSubMachine = sbRecvStr[2] == '+';
                machineUsageDisplay.IsCollapsed = sbRecvStr[1] == '+';

                // cut - including reserve (3chars)
                sbRecvStr.Remove(0, 5);
                elementLength -= 5;

                // add parsed usages to machine
                machineUsageDisplay.Usages = new List<ItxSingleMachineUsage>(machineUsageHead.NumberOfColumns);
                machineUsageDisplay.Usages.AddRange(usages.GetRange(parsedUsageOffset, machineUsageHead.NumberOfColumns));

                // now increment the parsedUsageOffset
                parsedUsageOffset += machineUsageHead.NumberOfColumns;

                // add to list of machine
                machineUsageHead.SingleMachine.Add(machineUsageDisplay);
            }

            mapGraphics.MachineUsage = machineUsageHead;
            return elementLength;
        }
    }
}
